const knex = require("../db"),
    dayjs = require("dayjs");

class FileService {
    //上传文件
    async create({ id, originalname, size, mimetype }) {
        try {
            const data = {
                id,
                file_name: originalname.split(".")[0],
                file_suffix: originalname.split(".")[1],
                file_size: size,
                file_url: originalname,
                mime_type: mimetype,
                create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
            };
            const result = await knex("files").where("file_name", originalname.split(".")[0]).first();
            if (!result) {
                await knex("files").insert(data);
            } else {
                throw new Error("文件已存在");
            }
            return data;
        } catch (error) {
            throw error;
        }
    }
}

module.exports = new FileService();
